package com.guessnumber.guess.chestinterface;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class myinv {
    static public class widget{
        int slot;
        ItemStack general;
        Material generalmat;
        ItemMeta generalim;
        boolean ignore;
        public widget(int s){
            general = new items().item_;
            this.slot = s;
        }
        public widget (int s,ItemStack g){
            slot = s;
            general = g;
            generalmat = g.getType();
            generalim = g.getItemMeta();
        }
        public widget (int s,Material gm,ItemMeta gim,int n){
            slot = s;
            general = new ItemStack(gm,n);
            general.setItemMeta(gim);
        }
        public widget (int x,int y,Material gm,ItemMeta gim,int n){
            slot = x + (9 * y) - 10;
            general = new ItemStack(gm,n);
            general.setItemMeta(gim);
        }
        public widget (int x, int y, Material gm,String name,List<String> lore,int custommodeldata,int n){
            slot = x + (9 * y) - 10;
            general = new ItemStack(gm,n);
            ItemMeta gim = general.getItemMeta();
            gim.setDisplayName(name);
            gim.setCustomModelData(custommodeldata);
            gim.setLore(lore);
            general.setItemMeta(gim);
        }
        public InventoryClickEvent WhenClick(InventoryClickEvent ice){
            ice.setCancelled(true);
            return ice;
        };
    }
    public Inventory general;
    public class myinvholder implements InventoryHolder {
        public Inventory inv;
        public String passw;
        public ArrayList<widget> generalwid;
        public int[] indexmap;
        public myinvholder(String password){
            this.inv = Bukkit.createInventory(this,18);
            this.passw = password;
            //this.generalwid = generalwidget;
        }
        public myinvholder(String password,ArrayList<widget> generalwidget,int[] i){
            this.inv = Bukkit.createInventory(this,18);
            this.passw = password;
            this.generalwid = generalwidget;
            this.indexmap = i;
        }
        public void setgeneralwidget(ArrayList<widget> gwid){
            this.generalwid = gwid;
        }
        @Override
        public @NotNull Inventory getInventory() {
            return this.inv;
        }
    }
    private ArrayList<widget> widgets = new ArrayList<>();
    private String n;
    private int[] indexmap = new int[54]; //indexmap[槽位] => 物品序号
    public myinv(int height,String name){
        this.n = name;
        general = Bukkit.createInventory(new myinvholder("HWFG'S INVENTORY"),(height * 9),n);
        for (int i = 0;i < 54;i++) indexmap[i] = -1;
    }
    public myinv(int height,String name,String password){
        this.n = name;
        general = Bukkit.createInventory(new myinvholder(password),(height * 9),n);
        for (int i = 0;i < 54;i++) indexmap[i] = -1;
    }
    public void AddWidget(widget widget){
        indexmap[widget.slot] = widgets.size();
        widgets.add(widget);
    }
    public void DeleteWidget(int x,int y){
        int slot = x + (9 * y) - 10;
        widgets.remove(indexmap[slot]);
        indexmap[slot] = -1;
        for (int i = 0;i < 54;i++) if (indexmap[i] > slot) indexmap[i]--;
    }
    public void DeleteWidget(int slot){
        widgets.remove(indexmap[slot]);
        indexmap[slot] = -1;
        for (int i = 0;i < 54;i++) if (indexmap[i] > slot) indexmap[i]--;
    }
    public void DeleteWidgetWithoutDeleteIndex(int slot){
        widgets.remove(slot);
    }
    public void fill(){
        for (int i = 0;i < 54;i++){
            if (indexmap[i] == 0){
                items it = new items();
                widget w = new widget(i,it.item_);
                AddWidget(w);
            }
        }
    }
    public Inventory pack(){
        general = Bukkit.createInventory(new myinvholder("HWFG'S INVENTORY",widgets,indexmap),general.getSize(),n);
        this.fill();
        for (widget w:widgets){
            general.setItem(w.slot,w.general);
        }
        return general;
    }
}
